package restaurant;

import java.util.ArrayList;
import java.util.Date;

/**
 * Represents offers that the restaurant provides. The {@code SpecialOffers}
 * class is a collection of {@code Offer} objects. In order to access an
 * {@code Offer}, the program must go through the {@code SpecialOffers} class.
 * 
 * @author Odessa
 * @see Offer
 */

public class SpecialOffers
{

	/** The list of offers. */
	private ArrayList<Offer> specialOffers;

	/** The offer number. */
	private int offerNumber = 1;



	/**
	 * Instantiates the special offers.
	 */
	public SpecialOffers() {
		specialOffers = new ArrayList<Offer>();
	}



	/**
	 * Creates a new offer. The {@code offerNumber} is used so that each offer
	 * created has a unique offer number. After the offer is created, it is
	 * added to the specialOffers and the {@code offerNumber} gets incremented.
	 * 
	 * @param startDate the start date of this offer.
	 * @param endDate the end date of this offer.
	 * @param description the description of this offer.
	 * @param discount the discount provided by this offer.
	 * @return the newly created offer.
	 */
	public Offer createOffer(Date startDate, Date endDate, String description,
			double discount)
	{
		Offer o = new Offer(offerNumber, startDate, endDate, description,
				discount);
		add(o);
		offerNumber++;

		return o;
	}



	/**
	 * Updates an offer. It first deletes the offer from the special offers and
	 * then creates a new offer containing the new information passed to the
	 * method. Then, the newly created offer is added to the specialOffers.
	 * 
	 * @param offerNo the offer number of the offer to be updated.
	 * @param startDate the start date of this offer.
	 * @param endDate the end date of this offer.
	 * @param description the description of this offer.
	 * @param discount the discount provided by this offer.
	 * @return the updated offer.
	 */
	public Offer updateOffer(int offerNo, Date startDate, Date endDate,
			String description, double discount)
	{
		delete(offerNo);
		Offer o = new Offer(offerNo, startDate, endDate, description, discount);
		add(o);

		return o;
	}



	/**
	 * Adds the {@code offer} to the specialOffers ArrayList. Takes an offer and
	 * appends it to the end of the list.
	 * 
	 * @param offer the offer to be added to the specialOffers.
	 */
	private void add(Offer offer)
	{
		specialOffers.add(offer);
	}



	/**
	 * Deletes an offer. It first finds the offer in the specialOffers and then
	 * deletes it.
	 * 
	 * @param offerNo the offer number of the offer to be deleted.
	 */
	public void delete(int offerNo)
	{
		Offer offer = find(offerNo);
		specialOffers.remove(offer);
	}



	/**
	 * Finds an offer. It looks at all the offers in the specialOffers and
	 * returns the offer with the specified offer number.
	 * 
	 * @param offerNo the offer number of the offer to find.
	 * @return the offer matching the offer number entered.
	 */
	private Offer find(int offerNo)
	{
		Offer offer = null;

		for (Offer o : specialOffers) {
			int id = o.getOfferNo();

			if (id == offerNo) {
				offer = o;
				break;
			}
		}

		return offer;
	}



	/**
	 * Gets the special offers.
	 * 
	 * @return the list of special offers.
	 */
	public ArrayList<Offer> getOffers()
	{
		return specialOffers;
	}



	/**
	 * Returns a string representation of the SpecialOffers.
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString()
	{
		String str = null;
		String delimter = "*";

		for (Offer o : specialOffers)
			str = o.getOfferNo() + delimter + o.getDescription() + delimter +
					o.getDiscount() + delimter + o.getStarts() + delimter +
					o.getEnds() + "\n";

		return str;
	}
}
